package cn.tianya.irock.data.model;


import java.io.Serializable;

import cn.tianya.irock.bll.Roles;
import cn.tianya.irock.core.Configuration;
import cn.tianya.irock.core.DateTime;

public class User implements Serializable {
	
	private int userId;
	private int key;
	
	private boolean isAnonymous = true;
	private String username;
	private String language;
	private String ipLocation;
	//private UserAccountStatus accountStatus;
	
	private static RolesConfiguration roles;

	static {
		roles = Configuration.config().getRolesConfiguration();
    }
	
	public User() {
		this(1, "guest", 0);
	}
	
	public User(int userId, String username, int key) {
		this.userId = userId;
		this.username = username;
		this.key = key;
		this.language = "";
	}
	
	public boolean isInRoles(String[] roleNames) {
		String [] userRoles = Roles.getUserRoleNames(userId);
		for (String userRole : userRoles ) {
			for (String roleName : roleNames ) {
				if( roleName.equals(userRole) )
					return true;
			}
		}
		return false;
	}
	
	public int getUserId() {
		return userId;
	}
	public void setUserId(int value) {
		this.userId = value;
	}
	
	public int getKey() {
		return key;
	}
	public void setKey(int value) {
		this.key = value;
	}
	
	public boolean isAnonymous() {
		return isAnonymous;
	}
	public void isAnonymous(boolean value) {
		isAnonymous = value;
	}
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String value) {
		this.username = value;
	}
	
	public String getIPLocation() {
		return ipLocation;
	}
	public void setIPLocation(String ipLocation) {
		this.ipLocation = ipLocation;
	}
	
	public DateTime getBannedUntil() {
		return null;
	}
	public void setBannedUntil(DateTime value) {
		
	}
	
	//public UserAccountStatus getAccountStatus() {
	//	return accountStatus;
	//}
	//public void setAccountStatus(UserAccountStatus value) {
	//	accountStatus = value;
	//}
	
	public boolean isAdministrator() {
		return isInRoles( new String[] { roles.getAdministrators() } );
	}

	public boolean isModerator() {
		return isInRoles( new String[] { roles.getModerators() } );
	}
	
	public boolean isSectionAdministrator() {
		return isInRoles( new String[] { roles.getSectionsAdministrators() } );
	}

	public boolean isEditor() {
		return isInRoles( new String[] { roles.getEditors() } );
	}
	
	//public boolean isBanned() {
	//	return accountStatus == UserAccountStatus.Banned;
	//}
	
	public String getLanguage() {	
		if ("".equals(language)) {
			language = Configuration.config().getDefaultLanguage();
		}
		
		return language;
	}
}
